package com.yjht.online.generate.impl.person.plat.wyx;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.yjht.online.constants.PathConstant;
import com.yjht.online.generate.GlobalContext;
import com.yjht.online.generate.impl.person.PersonGenerateProssorTemplate;
import com.yjht.online.generate.model.person.PersonModel;
import com.yjht.online.generate.model.person.SysRole;
import com.yjht.online.generate.person.PersonGenerateProssor;

import java.io.File;
import java.util.List;

public class PersonGenerateProssorWyx extends PersonGenerateProssorTemplate {

    @Override
    protected List<PersonModel> hanlerModel(List<PersonModel> personModels) {

        String qywx = deductionQyh();

        for (PersonModel personModel : personModels) {
            String roleId = deductionRoleId(personModel.getRoleName());
            deductionRoleId(personModel.getRoleName());
            personModel.setRoleId(roleId);
            personModel.setQyh(qywx);
            if(StrUtil.isBlank(personModel.getEmail())){
                personModel.setEmail(randomEmail());
            }

        }

        return personModels;
    }

    @Override
    protected String templatePath() {
        return "templates/wyx/user.xlsx";
    }

    @Override
    protected String fullOutputPath() {
        String outputPath = PathConstant.WYX_PATH + StrUtil.SLASH  + "SYS_USER.xlsx";
        return outputPath;
    }

    /**
     * 推断企业号名字
     * @return
     */
    private String deductionQyh(){
        String qywx = "";
        if(GlobalContext.getCorpInfo()!=null && StrUtil.isNotBlank(GlobalContext.getCorpInfo().getCorpName())){
            qywx = GlobalContext.getCorpInfo().getCorpName();
            String  shortName = StrUtil.subAfter(qywx,"村镇银行",true);
            if(StrUtil.isBlank(shortName)){
                shortName =  StrUtil.subBefore(qywx,"村镇银行",true);
            }
            if(StrUtil.isBlank(shortName)){
                shortName = qywx;
            }
            qywx = shortName+"企业号";
        }
        return qywx;
    }

    /**
     * 推断角色id
     * @param roleName
     * @return
     */
    private String deductionRoleId(String roleName) {

        List<SysRole> sysRoles = GlobalContext.getGetWyxSysRoles();

        for (SysRole sysRole : sysRoles) {
            if(sysRole.getRoleName().contains(roleName)){
                return  sysRole.getRoleId();
            }
        }
        return roleName;
    }

    private String randomEmail(){
        return RandomUtil.randomNumbers(9)+"@sina.com";
    }
}
